package com.itzdm.service.impl;

import com.itzdm.mapper.CarMapper;
import com.itzdm.mapper.UserMapper;
import com.itzdm.pojo.Car;
import com.itzdm.pojo.Page;
import com.itzdm.pojo.PageBean;
import com.itzdm.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageBean page(Page page) {
        Long count = carMapper.count();
        page.setStart(page.getPageNumber() * page.getPageSize());
        List<Car> carList = carMapper.pageSelect(page);
        return new PageBean(count, carList);
    }

    @Override
    public Double payMoney(Integer userId, Integer vehicleId, Integer days) {
        Double needMoney = getNeedMoney(vehicleId,days);
        Double remindMoney = userMapper.getRemindMoney(userId);
        if (remindMoney < needMoney){
            throw new RuntimeException("余额不足!!");
        }
        userMapper.updateRemindMoney(remindMoney - needMoney,userId);
        return needMoney;
    }

    @Override
    public void addVehicleStatus(Integer userId, String beginTime, String deadline) {
        carMapper.addVehicleStatus(userId,beginTime,deadline);
    }

    @Override
    public Integer getStatusId(Integer userId, String beginTime, String deadline) {
        return carMapper.getVehicleStatusId(userId,beginTime,deadline);
    }

    @Override
    public void updateVehicleStatus(Integer vehicleId, Integer vehicleStatusId) {
        carMapper.updateStatus(vehicleId,vehicleStatusId);
    }

    private Double getNeedMoney(Integer vehicleId, Integer days) {
        Double perRent = carMapper.getPerRent(vehicleId);
        return perRent * days;
    }
}
